package com.whq.common;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * 字符串操作工具类
 * 
 * @author yangyingpeng 2008-11-18
 * 
 */

public class StringUtil {
	/**
	 * 分解字符串，并且剔除空的元素。
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static List<String> split2list(String str, String regex) {
		List<String> retList = new ArrayList<String>();
		if (str == null) {
			return retList;
		}
		String[] words = str.split(regex);
		for (int i = 0; i < words.length; i++) {
			if ((words[i] != null) && (!words[i].trim().equals(""))) {
				retList.add(words[i]);
			}
		}
		return retList;
	}

	/**
	 * 分解字符串，并且剔除空的元素。
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static Set<String> split2set(String str, String regex) {
		Set<String> retList = new LinkedHashSet<String>();
		if (str == null) {
			return retList;
		}
		String[] words = str.split(regex);
		for (int i = 0; i < words.length; i++) {
			if ((words[i] != null) && (!words[i].trim().equals(""))) {
				retList.add(words[i]);
			}
		}
		return retList;
	}

	/**
	 * 
	 * 分解字符串，并且剔除空的元素。
	 * 
	 * 例如： String str = ",a,b,,c,"; String[] rsStr = StringUtil.split(str, ",");
	 * 所得结果是 a b c
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static String[] split(String str, String regex) {
		if (str == null) {
			return null;
		}
		List<String> notNullWords = split2list(str, regex);

		String[] rsWords = new String[notNullWords.size()];
		for (int i = 0; i < rsWords.length; i++) {
			rsWords[i] = notNullWords.get(i);
		}

		return rsWords;
	}

	/**
	 * 计算某个字符串的字节长度，字母等 ASCII 字符计为一个长度，汉字等全角字符计为两个长度。
	 * 
	 * @param str
	 * @return
	 * 
	 * @author Gao Baowen 2009-3-27 下午03:16:22
	 */
	public static int getStringByteLength(String str) {
		if (str == null || str.length() == 0) {
			return 0;
		}
		int count = 0;
		char[] chs = str.toCharArray();
		for (int i = 0; i < chs.length; i++) {
			count += (chs[i] > 0xff) ? 2 : 1;
		}
		return count;
	}

	/**
	 * 判断一个字符串中包含另外一个字符串数组元素的个数
	 * 
	 * @param origStr
	 * @param arr
	 * @return
	 */
	public static int findArrUnitCountInStr(String origStr, String[] arr) {
		int numCount = 0;
		String testedNick = "_" + origStr + "_";
		for (int i = 0; i < arr.length; i++) {
			String str = arr[i].trim();
			if (!str.equals("")) {
				numCount += testedNick.split(str).length - 1;
			}
		}
		return numCount;
	}

	/**
	 * 分解字符串，并且剔除空和重复的元素。
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static List<String> split2DistinctList(String str, String regex) {
		List<String> retList = split2list(str, regex);
		if (retList == null) {
			return new ArrayList<String>(0);
		}
		Set<String> tmpSet = new HashSet<String>(0);
		for (Iterator<String> iterator = retList.iterator(); iterator.hasNext();) {
			String oneItem = (String) iterator.next();
			if (tmpSet.contains(oneItem)) {
				iterator.remove();
			} else {
				tmpSet.add(oneItem);
			}
		}
		return retList;
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static boolean isBlank(String str) {
		if (str == null || "".equals(str.trim()) || "null".equals(str.trim().toLowerCase())) {
			return true;
		}
		return false;
	}

	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}

	/**
	 * 判断是否是邮箱
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (email == null || "".equals(email)) {
			return false;
		} else {
			Pattern p = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
			Matcher m = p.matcher(email);
			return m.matches();
		}
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[\\+0-9]*");
		return pattern.matcher(str).matches();
	}

	/**
	 * 将回车，换行符替换成空格
	 * 
	 * @param orig
	 * @return
	 */
	public static String replaceLineBreak2Space(String orig) {
		if (orig == null) {
			return "";
		}
		String ret = orig;
		ret = ret.replaceAll("\r\n", " ");
		ret = ret.replaceAll("\n", " ");
		ret = ret.replaceAll("\r", " ");
		return ret;
	}

	public static boolean isNotEmpty(String str) {
		return !StringUtil.isEmpty(str);
	}

	/**
	 * 判断是否是手机号码
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		if (mobiles == null || "".equals(mobiles)) {
			return false;
		} else {
			Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
			Matcher m = p.matcher(mobiles);
			return m.matches();
		}
	}

	public static boolean isPinYin(String str) {
		Pattern pattern = Pattern.compile("[ a-zA-Z]*");
		return pattern.matcher(str).matches();
	}

	public static boolean containCn(String str) {
		Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
		return pattern.matcher(str).find();
	}

	public static String formatAmount(double amount) {
		return new DecimalFormat(",###").format(amount);
	}

	public static Vector<Integer> findStringIndex(String source, String sub) {
		Vector<Integer> vec = new Vector<Integer>();

		int from = 0;
		while (true) {
			int i = source.indexOf(sub, from);
			if (i == -1) {
				break;
			}
			vec.add(i);
			from = i + sub.length();
		}

		return vec;
	}

	public static String getFormatNum(String str) {
		DecimalFormat df = new DecimalFormat("###,###");
		return df.format(Double.parseDouble(str));
	}

	/**
	 * main方法 。
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		List<String> tmp = split2DistinctList("1,2,3,4,5,4,5,3,2,1", ",");
		System.out.println(tmp);
	}

}
